﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Domain.DTO;

namespace W.Warehouse.Api.Controllers
{
    /// <summary>
    /// 入库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class StorageController : ControllerBase
    {
        private readonly IMediator _mediator;
        public StorageController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加入库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddStorage(StorageCreateCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.UpdateName = name;
            command.user_id = Convert.ToInt32(uid);
            command.office_id = sid;
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 获取名称
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> Uid()
        {
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            return name;
        }
        /// <summary>
        /// 删除入库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelectStorage(StorageDelectCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 批量删除入库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelectRangeStorage(StorageDelectRangeCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 修改入库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateStorage(StorageUpdateCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.UpdateName = name;
            command.office_id= sid;
            command.user_id= Convert.ToInt32(uid);
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 审核入库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AuditStorage(StorageAuditCommand command)
        {
            var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
            command.ExaminePeople = name;
            return await _mediator.Send(command);
        }
    }
}
